//
//  HHNormalTableListCell.m
//  developKit
//
//  Created by Hayder on 2022/12/17.
//

#import "HHNormalTableListCell.h"

@implementation HHNormalTableListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.blankView addSubview:self.iconImageView];
        [self.blankView addSubview:self.titleLabel];
        [self.blankView addSubview:self.descLabel];
        [self.blankView addSubview:self.arrowImageView];
    }
    
    return self;
}

- (void)setItem:(HHNormalListItem *)item
{
    _item = item;
    
    [self setConfigWithItem:item];
    
    self.titleLabel.text = item.title;
    if(item.iconImage)
    {
        if([item.iconImage isKindOfClass:[NSString class]])
        {
            [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:item.iconImage]];
        }else if([item.iconImage isKindOfClass:[UIImage class]])
        {
            self.iconImageView.image = item.iconImage;
        }
    }
    self.descLabel.text = item.descText;
    self.descLabel.textAlignment = item.descTextAlignment;
    self.arrowImageView.hidden = item.isShowArrow?NO:YES;
    [self UIHidden];
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat contentW = self.blankView.width;
    CGFloat contentH = self.blankView.height;
    self.arrowImageView.frame = CGRectMake(contentW-25-self.margin, (contentH-25)/2, 25, 25);
    self.iconImageView.frame = CGRectMake(self.margin, 5, 40, 40);
    //1.只有title的情况
    if(self.item.title && !self.item.iconImage && !self.item.descText)
    {
        if(self.item.isShowArrow)
        {
            self.titleLabel.frame = CGRectMake(self.margin, 0, self.arrowImageView.left - 2*self.margin, contentH);
        }else
        {
            self.titleLabel.frame = CGRectMake(self.margin, 0, contentW - 2*self.margin, contentH);
        }
    }
    
    //2 有icon 和 title 是一种情况
    if(self.item.iconImage && self.titleLabel)
    {
        if(self.item.isShowArrow)
        {
            self.titleLabel.frame = CGRectMake(self.iconImageView.right+self.margin, 0, self.arrowImageView.left - self.iconImageView.right - 2*self.margin, contentH);
        }else
        {
            self.titleLabel.frame = CGRectMake(self.iconImageView.right+self.margin, 0, contentW - self.iconImageView.right - 2*self.margin, contentH);
        }
    }
    
    //3.有title  descLabel
    if(self.item.title && self.item.descText)
    {
        if(self.item.isShowArrow)
        {
            self.titleLabel.frame = CGRectMake(self.margin, 0, self.item.titleDefaultWidth, contentH);
            self.descLabel.frame = CGRectMake(self.titleLabel.right + self.margin, 0, self.arrowImageView.left - self.titleLabel.right - self.margin, contentH);
        }else
        {
            self.titleLabel.frame = CGRectMake(self.margin, 0, self.item.titleDefaultWidth, contentH);
            self.descLabel.frame = CGRectMake(self.titleLabel.right + self.margin, 0, contentW - self.titleLabel.right - 2*self.margin, contentH);
        }
    }
}

- (void)UIHidden
{
    //1.只有title的情况
    if(self.item.title && !self.item.iconImage && !self.item.descText)
    {
        if(self.item.isShowArrow)
        {
            self.iconImageView.hidden = YES;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = YES;
            self.arrowImageView.hidden = NO;
        }else
        {
            self.iconImageView.hidden = YES;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = YES;
            self.arrowImageView.hidden = YES;
        }
    }
    
    //2 有icon 和 title 是一种情况
    if(self.item.iconImage && self.titleLabel)
    {
        if(self.item.isShowArrow)
        {
            self.iconImageView.hidden = NO;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = YES;
            self.arrowImageView.hidden = NO;
        }else
        {
            self.iconImageView.hidden = NO;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = YES;
            self.arrowImageView.hidden = YES;
        }
    }
    
    //3.有title  descLabel
    if(self.item.title && self.item.descText)
    {
        if(self.item.isShowArrow)
        {
            self.iconImageView.hidden = YES;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = NO;
            self.arrowImageView.hidden = NO;
        }else
        {
            self.iconImageView.hidden = YES;
            self.titleLabel.hidden = NO;
            self.descLabel.hidden = NO;
            self.arrowImageView.hidden = YES;
        }
    }
}

- (UIImageView *)iconImageView
{
    if(!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        [_iconImageView cornerRadius:20];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = Font(15);
        _titleLabel.textColor = kGray_51;
    }
    
    return _titleLabel;
}

- (UILabel *)descLabel
{
    if(!_descLabel)
    {
        _descLabel = [[UILabel alloc] init];
        _descLabel.font = Font(14);
        _descLabel.textColor = kGray_150;
    }
    
    return _descLabel;
}

- (UIImageView *)arrowImageView
{
    if(!_arrowImageView)
    {
        UIImageView *arrowImageView = [[UIImageView alloc] init];
        arrowImageView.image = BundlePngImage(@"hh_arrow");
        _arrowImageView = arrowImageView;
    }
    return _arrowImageView;
}

@end
